<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col
            :md="5"
            :sm="15"
          >
            <a-form-item label="用户角色">
              <a-select
                placeholder="请选择"
                v-model="queryParam.userRoles"
                default-value="0"
              >
                <a-select-option :value="''">全部</a-select-option>
                <a-select-option :value="0">管理员</a-select-option>
                <a-select-option :value="1">录入员</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col
            :md="5"
            :sm="15"
          >
            <a-form-item label="所属机构">
              <a-input
                placeholder="请输入"
                v-model="queryParam.orgNo"
              />
            </a-form-item>
          </a-col>
          <a-col
            :md="5"
            :sm="15"
          >
            <a-form-item label="用户名称">
              <a-input
                placeholder="请输入"
                v-model="queryParam.useName"
              />
            </a-form-item>
          </a-col>
          <a-col
            :md="5"
            :sm="15"
          >
            <a-form-item label="手机号">
              <a-input
                placeholder="请输入"
                v-model="queryParam.phone"
              />
            </a-form-item>
          </a-col>
          <a-col
            :md="4"
            :sm="24"
          >
            <span class="table-page-search-submitButtons">
              <a-button
                type="primary"
                @click="$refs.table.refresh(true)"
              >查询</a-button>
              <a-button
                style="margin-left: 8px"
                @click="() => queryParam = {}"
              >重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <s-table
      size="default"
      ref="table"
      rowKey="roleId"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      :columns="columns"
      :data="loadData"
      defaultSort="createTime"
    >
      <span
        slot="id"
        slot-scope="text,record,index"
      >
        {{ index + 1 }}
      </span>
      <span
        slot="action"
        slot-scope="text, record"
      >
        <a @click="handleEditpreview(record)">预览</a>
        <a-divider type="vertical" />
        <a @click="handleEdit(record)">编辑</a>
        <a-divider type="vertical" />
        <a @click="delByIds([record.id])">删除</a>
      </span>
    </s-table>
    <div class="footer">
      <a-button
        type="primary"
        @click="addUse"
        class="addUseButton"
      >新建用户</a-button>
    </div>
    <useCreateModal
      ref="modal"
      @ok="handleOk"
    />
    <role-scope-modal
      ref="scopemodal"
      @ok="handleOk"
    />
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { getRoleList, delRole, changRoleStatus } from '@/api/system'
import useCreateModal from '../modules/useCreateModal.vue'
import RoleScopeModal from '../modules/RoleScopeModal.vue'
import pick from 'lodash.pick'
import { checkPermission } from '@/utils/permissions'
export default {
  name: 'AddUser',
  components: {
    STable,
    useCreateModal,
    RoleScopeModal
  },
  data () {
    return {
      description: '列表使用场景：后台管理中的权限管理以及角色管理，可用于基于 RBAC 设计的角色权限控制，颗粒度细到每一个操作类型。',
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      form: this.$form.createForm(this),
      mdl: {},
      permissions: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'id' },
          dataIndex: 'roleId'

        },
        {
          title: '用户角色',
          dataIndex: 'roleName'
        },
        {
          title: '所属机构',
          dataIndex: 'orgName'
        },
        {
          title: '用户名称',
          dataIndex: 'name'
        },
        {
          title: '手机号',
          dataIndex: 'phone'
        },
        {
          title: '用户名',
          dataIndex: 'userName'
        }, {
          title: '操作',
          width: '200px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getRoleList(Object.assign(parameter, this.queryParam))
      },
      selectedRowKeys: [],
      selectedRows: [],
      addEnable: checkPermission('system:role:add'),
      editEnabel: checkPermission('system:role:edit'),
      removeEnable: checkPermission('system:role:remove')
    }
  },
  created () {
  },
  methods: {
    onSelectChange (selectedRowKeys) {
      console.log('selectedRowKeys changed: ', selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
    },
    handleAdd (parentId) {
      this.$refs.modal.add(parentId)
    },
    handleEdit (record) {
      this.$refs.modal.edit(record)
    },
    handleScope (record) {
      this.$refs.scopemodal.edit(record)
    },
    onChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleOk () {
      this.$refs.table.refresh(true)
      console.log('handleSaveOk')
    },
    delByIds (ids) {
      delRole({ ids: ids.join(',') }).then(res => {
        if (res.code === 0) {
          this.$message.success(`删除成功`)
          this.handleOk()
        } else {
          this.$message.error(res.msg)
        }
        // const difference = new Set(this.selectedRowKeys.filter(x => !new Set(ids).has(x)))
        // this.selectedRowKeys = Array.from(difference)
        this.selectedRowKeys = []
      })
    },
    onChangeStatus (record) {
      record.status = record.status === '0' ? '1' : '0'
      changRoleStatus(pick(record, 'roleId', 'status'))
      // 发送状态到服务器
    },
    addUse () {
      console.log('我点击了新建用户')
      this.$refs.modal.add()
    },
    handleEditpreview (record) {
      console.log('我点击了预览')
      this.$refs.modal.edit(record, true)
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
<style scoped>
.footer {
  width: 100%;
  margin-top: 30px;
  text-align: center;
}
</style>
